<?php 
namespace app\index\controller;

use think\Controller;
use app\common\controller\Curls;

class Base extends Controller
{
	protected $request;
	
	protected $staff_id;
	protected $openid;
	private $appId = 'wxfe8a528cd8fc968d';
	private $appSecret = '6003847226b40fa38f032d143d64ce35';
	public function _initialize()
	{
		$this->staff_id = session('staff_id');
		$this->openid = session('openid');
		$this->request = request();
	}
	/**
	 * 获取ACCESS_TOKEN
	 * @return mixed
	 */
	protected function getToken()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
		$res = json_decode(Curls::curl($url),true);
		return $res['access_token'];
	}
	/**
	 * 获取两个经纬度之间的距离
	 * @param  string $lat1 纬一
	 * @param  String $lng1 经一
	 * @param  String $lat2 纬二
	 * @param  String $lng2 经二
	 * @return float  返回两点之间的距离
	 */
	protected function calcDistance($lat1, $lng1, $lat2, $lng2) {
		// 将角度转为狐度
		$EARTH_RADIUS = 6378137;   //地球半径
		$RAD = pi() / 180.0;
		
		$radLat1 = $lat1 * $RAD;
		$radLat2 = $lat2 * $RAD;
		$a = $radLat1 - $radLat2;    // 两点纬度差
		$b = ($lng1 - $lng2) * $RAD;  // 两点经度差
		$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
		$s = $s * $EARTH_RADIUS;
		$s = round($s * 10000) / 10000;
		return $s;
	}
}